package com.meishij.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.annotations.SerializedName;
import com.meishij.R;
import com.meishij.entry.MallSellEntry;

import java.util.List;

/**
 * Created by 肖荣 on 2016/10/15.
 */

public class MallItemOrderAdapter extends RecyclerView.Adapter<MallItemOrderAdapter.ViewHolder> {
    private Context context;
    private List<Object> list;
    private int[] layoutId;

    public MallItemOrderAdapter(Context context, List<Object> list, int[] layoutId) {
        this.context = context;
        this.list = list;
        this.layoutId = layoutId;
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0){
            //recycler
            return layoutId[0];
        }else if(position==1){
            //查看更多
            return layoutId[1];
        }else if(position==2){
            //logo
            return layoutId[2];
        }else {
            //wenzi
            return layoutId[3];
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, viewType, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if(position==0){
            //加载recycler,但是如果传过来的shaidanlist是空的,就直接加载一个图片即可
            List<MallSellEntry.ObjBean.ShaidanBean> shaidanBeen = (List<MallSellEntry.ObjBean.ShaidanBean>) list.get(0);
            GridLayoutManager layoutManager = (GridLayoutManager) holder.mall_sell_shaidan_recycler.getLayoutManager();
            layoutManager.setSpanCount(1);
            if(shaidanBeen==null || shaidanBeen.size() == 0){
                holder.mall_sell_shaidan_recycler.setVisibility(View.GONE);
            }else{//如果有数据
                holder.mall_sell_shaidan_recycler.setVisibility(View.VISIBLE);
                MallItemShaiDanAdapter mallItemShaiDanAdapter = new MallItemShaiDanAdapter(context, shaidanBeen);
                holder.mall_sell_shaidan_recycler.setAdapter(mallItemShaiDanAdapter);
                holder.mall_sell_shaidan_noicon.setVisibility(View.GONE);
            }
        } else if(position==2){
            //加载logo
            List<String> logo = (List<String>) this.list.get(2);
            String logoUrl = logo.get(0);
            holder.mall_sell_item__bottom_logo.setImageURI(logoUrl);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{


        private SimpleDraweeView mall_sell_item__bottom_logo;
        private RecyclerView mall_sell_shaidan_recycler;
        private ImageView mall_sell_shaidan_noicon;

        public ViewHolder(View itemView) {
            super(itemView);
            mall_sell_shaidan_recycler = ((RecyclerView) itemView.findViewById(R.id.mall_sell_shaidan_recycler));
            mall_sell_shaidan_noicon = ((ImageView) itemView.findViewById(R.id.mall_sell_shaidan_noicon));
            mall_sell_item__bottom_logo = ((SimpleDraweeView) itemView.findViewById(R.id.mall_sell_item__bottom_logo));
        }
    }
}
